package com.flyme.module.base.provider.controller;


import com.flyme.common.core.model.ResultBody;
import com.flyme.common.core.model.query.CriteriaQuery;
import com.flyme.common.core.security.OpenHelper;
import com.flyme.common.core.security.OpenUserAuth;
import com.flyme.core.base.supper.controller.BaseController;
import com.flyme.core.base.supper.entity.EntityMap;
import com.flyme.module.base.client.api.SysDeptRemoteService;
import com.flyme.module.base.client.entity.SysDept;
import com.flyme.module.base.provider.service.SysDeptService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


@Slf4j
@Api(tags = "部门管理")
@RestController
public class SysDeptController extends BaseController<SysDeptService, SysDept> implements SysDeptRemoteService {

    @ApiOperation(value = "根据parentId查询部门", notes = "根据parentId查询部门")
    @Override
    public ResultBody getByParentId(@PathVariable String parentId) {
        OpenUserAuth userAuth = OpenHelper.getUserAuth();
        CriteriaQuery qw = new CriteriaQuery<EntityMap>();
        qw.select("d.id value", "d.deptName label", "isParent", "parentId", "headerId", "u.userName");
        qw.eq("parentId", parentId);
        qw.eq("d.companyId", userAuth.getCompanyId());
        qw.createAlias("user", "u");
        List list = bizService.listEntityMaps(qw);
        return ResultBody.success(toCascader(list));
    }

    @ApiOperation(value = "添加部门")
    @Override
    public ResultBody<Object> add(@ModelAttribute SysDept dept) {
        bizService.add(dept);
        return ResultBody.ok("添加成功");
    }

    @ApiOperation(value = "编辑部门")
    @Override
    public ResultBody<Object> edit(@ModelAttribute SysDept dept) {
        bizService.edit(dept);
        return ResultBody.ok("编辑成功");
    }

    @ApiOperation(value = "删除部门", notes = "删除部门")
    @Override
    public ResultBody delById(@PathVariable Long deptId) {
        return bizService.delById(deptId);
    }
}
